﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NCT.Framework;
using VISS.API.Users;

namespace VISS.Web.BackEnd.Modules.User {

    public partial class UserListPage : System.Web.UI.Page {

        protected void Page_Load(object sender, EventArgs e) {
            int pageNum = ConvertUtils.ToInt(Request.QueryString["page"], 1);

            string gender = Request.QueryString["gioitinh"];
            EUserGender selectedGender = (EUserGender)ConvertUtils.ToByte(gender, 2);

            string status = Request.QueryString["trangthai"];
            EUserStatus selectedStatus = (EUserStatus)ConvertUtils.ToShort(status, -1);

            bool haveAvatar = ConvertUtils.ToBool(Request.QueryString["avatar"], false);

            idUserListFilter.SelectedGender = selectedGender;
            idUserListFilter.SelectedStatus = selectedStatus;
            idUserListFilter.HaveAvatar = haveAvatar;

            GetUserList(pageNum, selectedGender, selectedStatus, haveAvatar);
        }

        private void GetUserList(int pageNum, EUserGender gender, EUserStatus status, bool haveAvatar) {

            IUserAdmin _objUser = StructureMapFactory.GetInstance<IUserAdmin>();

            bool hasParam = false;
            string queryString = string.Empty;

            long totalRecords;
            List<UserAdminEnt> lsUser = _objUser.GetWithConditionAndPaging(gender, status, haveAvatar, pageNum, AppSettings.RecordPerPage, out totalRecords);
            if (totalRecords == 0) {
                return;
            }

            if (gender != EUserGender.Unknown) {
                queryString += "gioitinh=" + (byte)gender;
                hasParam = true;
            }
            if (status != EUserStatus.Unknown) {
                if (hasParam) {
                    queryString += "&";
                }
                queryString += "trangthai=" + (short)status;
                hasParam = true;
            }
            if (haveAvatar) {
                if (hasParam) {
                    queryString += "&";
                }
                queryString += "avatar=true";
                hasParam = true;
            }

            if (hasParam) {
                queryString += "&";
            }

            idPaging.TotalRecord = totalRecords;
            idPaging.PageSize = AppSettings.RecordPerPage;
            idPaging.CurrentPage = pageNum;
            idPaging.TargetPage = "/users/danh_sach";
            idPaging.QueryString = queryString;

            idUserList.UserList = lsUser;
        }
    }
}
